<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
	response.setHeader("X-Frame-Options","SAMEORIGIN");
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<jsp:include page="../../public/public.jsp"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<!--上传  -->
<script type="text/javascript" src="<%=basePath%>them/jquery-easyui-1.4.2/ajaxfileupload.js"></script>
<script type="text/javascript">
  var wid = $(window).width();
  var hei = $(window).height();
  var i = 0;
  var basePath='<%=basePath%>';
  $(function(){
	  fn_queryHomePage();
  });
  /* 进入页面就查询首页大图 */
  function fn_queryHomePage(){
	  $.ajax({
			url:basePath+'homepage/queryHomePage',
			type:'post',
			data:{
				'homeType':'BANNER' /* 图片类型 */
			},
			error:function(){
				alert("提交出错！");
			},
			success:function(arr){
				$(arr).each(function(index,cm){
					  i++;
					  var html='<div title="Home" style="padding:2px;">'+
						'<img id="img_b_'+i+'" width="'+(wid-10)+'" alt="" src="'+basePath+'images/banner/'+cm['homeImg']+'">'+
						'<table width="100%">'+
						/* 
						去掉设置的链接路径
						'<tr>'+
						'<td align="center">'+
						'BANNER'+i+'图片链接:<input style="width:400px" id="txt_b_'+i+'" value="'+cm['homeUrl']+'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+
						'</td></tr>'+ */
						'<tr>'+
						'<td align="center">'+
						'BANNER'+i+'更改图片:<input id="file_b_'+i+'" name="file_b_'+i+'" style="width:400px" type="file" onChange="fn_upload(\''+i+'\')"> <font color="red">图片大小建议为：1001*236</font>'+
						'</td></tr>'+
						'<tr>'+
						'<td align="center">'+
						'<input type="hidden" id="hd_b_img_'+i+'" value="'+cm['homeImg']+'">'+
						'<input type="hidden" id="hd_b_id_'+i+'" value="'+cm['homeId']+'">'+
						'<input type="hidden" id="hd_url_'+i+'" value="'+basePath+'homepage/updateHomepage">'+
						'<a onclick="fn_save(\''+i+'\')" class="easyui-linkbutton" data-options="iconCls:\'icon-save\'">保存</a>'+
						'<a onclick="fn_del(\''+i+'\')" class="easyui-linkbutton" data-options="iconCls:\'icon-remove\'">删除</a>'+
						'</td></tr></table>'+
						'</div>';
					  $('#content').tabs('add',{  
				            title:'BANNER'+i,
				            content:html
				      }); 
				});
			}
	  })
  }
  function fn_upload(flag){
	  var url = basePath+'upload/fileUpload';
	  var suffixStr = "jpg,JPG,png,PNG,gif,GIF,bmp,BMP";
	  var savePath = ",images,banner";
	  $.ajaxFileUpload
      ({//String fileObjectId,int MaxSize,String suffixStr,String savePath,int width,int height
             url: url+'?fileObjectId=file_b_'+flag+'&MaxSize='+0+'&suffixStr='+suffixStr+'&savePath='+savePath+'&width='+0+'&height='+0, //用于文件上传的服务器端请求地址
             secureuri: false, //是否需要安全协议，一般设置为false
             fileElementId: 'file_b_'+flag, //文件上传域的ID
             dataType: 'json', //返回值类型 一般设置为json
             success: function (data, status)  //服务器成功响应处理函数
             {
                 if(data.success=="true"){
               	     $("#img_b_"+flag).attr("src","<%=basePath%>images/banner/"+data.imgurl);
               	     $("#hd_b_img_"+flag).val(data.imgurl);
                 }else{
               	    alert(data.msg);
                 }
             },
             error: function (data, status, e)//服务器响应失败处理函数
             {
                 alert(e);
             }
      })
  }
  function fn_save(flag){
	  var homeId = $("#hd_b_id_"+flag).val();
	  var homeImg = $("#hd_b_img_"+flag).val();
	  var homeUrl = $("#txt_b_"+flag).val();
	  var homeType = "BANNER";
	  var homeTitle = "";
	  var url = $("#hd_url_"+flag).val();
	  if(homeImg==''){
		  alert("请上传图片后保存");
		  return;
	  }
	  /*if(homeUrl==''){
		  alert("请填写图片跳转链接");
		  return;
	  }*/
	  $.ajax({
			url:url,
			type:'post',
			data:{
				homeId:homeId,
				homeImg:homeImg,
				homeUrl:homeUrl,
				homeType:homeType,
				homeTitle:homeTitle
			},
			error:function(){
				alert("提交出错！");
			},
			success:function(arr){
               alert(arr.title);
			}
	  })
  }
  function fn_del(flag){
	  $.ajax({
			url:basePath+'homepage/delHomePage',
			type:'post',
			data:{
				homeId:$("#hd_b_id_"+flag).val()
			},
			error:function(){
				alert("提交出错！");
			},
			success:function(arr){
               alert(arr.title);
               if(arr.title="成功"){
            	   var tab = $('#content').tabs('getSelected');
                   if(tab){
                       var index = $('#content').tabs('getTabIndex',tab);
                       $('#content').tabs('close',index);
                   }
               }
			}
	  })
  };
  function fn_new(){
	  i++;
	  var html='<div title="Home" style="padding:2px;">'+
		'<img id="img_b_'+i+'" width="'+(wid-10)+'" alt="" src="'+basePath+'images/noimg.png">'+
		'<table width="100%">'+
		/* 
		新增banner时，不需要图片的链接地址
		'<tr>'+
		'<td align="center">'+
		'BANNER'+i+'图片链接:<input style="width:400px" id="txt_b_'+i+'" value="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+
		'</td></tr>'+ */
		'<tr>'+
		'<td align="center">'+
		'BANNER'+i+'更改图片:<input id="file_b_'+i+'" name="file_b_'+i+'" style="width:400px" type="file" onChange="fn_upload(\''+i+'\')"> <font color="red">图片大小建议为：1001*236</font>'+
		'</td></tr>'+
		'<tr>'+
		'<td align="center">'+
		'<input type="hidden" id="hd_b_img_'+i+'" value="">'+
		'<input type="hidden" id="hd_b_id_'+i+'" value="">'+
		'<input type="hidden" id="hd_url_'+i+'" value="'+basePath+'homepage/addHomePage">'+
		'<a onclick="fn_save(\''+i+'\')" class="easyui-linkbutton" data-options="iconCls:\'icon-save\'">保存</a>'+
		'</td></tr></table>'+
		'</div>';
	  $('#content').tabs('add',{  
            title:'BANNER'+i,
            content:html
      }); 
  }
</script>
</head>
<body class="easyui-layout">

<div class="easyui-panel" title="首页BANNER管理" style="width:100%;">
	<div class="easyui-tabs" data-options="border:false,plain:true" style="width:100%" id="content">
		
	</div>
	
</div>	
<table width="100%">
<tr>
  <td align="center"><a onclick="fn_new()" class="easyui-linkbutton" data-options="iconCls:'icon-add'">新增</a></td>
</tr>
</table>
</body>
</html>